package com.niit.bc.servlet;

import com.niit.bc.bean.BookBean;
import com.niit.bc.bean.CartBean;
import com.niit.bc.biz.BookBiz;
import com.niit.bc.biz.impl.BookBizImpl;
import com.niit.bc.util.web.Constants;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class Cart extends HttpServlet {


    /**
     * @潘玉
     */
    private static final long serialVersionUID = 1L;

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        response.setContentType("text/html");
        response.setCharacterEncoding("utf8");
        request.setCharacterEncoding("utf8");
        CartBean cartBean = (CartBean) request.getSession().getAttribute(Constants.BOOK_SESSION_CART);

        if (cartBean == null) {
            request.getRequestDispatcher("cart.jsp").forward(request, response);
        } else {
            Map <Integer, Integer> myCart = cartBean.getmyCart();
            Iterator <Integer> iterator = myCart.keySet().iterator();
            BookBean bookBean = new BookBean();
            BookBiz bookBiz = new BookBizImpl();
            List <CartBean> cartList = new ArrayList <CartBean>();
            int bookId = 0;
            double cartAllPrice = 0;
            while (iterator.hasNext()) {
                bookId = iterator.next();
//System.out.println(bookId);//验证是否获得到了bookId
                if (bookId != 0) {
                    List <BookBean> list = bookBiz.findBookByBookId(bookId);
                    bookBean = list.get(0);
                    CartBean cart = new CartBean();
                    int bookCount = ((Integer) cartBean.myCart.get(bookId)).intValue();
                    ;
                    double bookAllPrice = bookCount * bookBean.getBookPrice();
                    cartAllPrice += bookAllPrice;
//System.out.println(bookBean.getBookId());
//System.out.println(bookBean.getBookName());
//System.out.println(bookBean.getBookPhoto());
//System.out.println(bookBean.getBookPrice());
//System.out.println(bookCount);

                    cart.setBookId(bookBean.getBookId());
                    cart.setBookNum(bookBean.getBookNum());
                    cart.setBookName(bookBean.getBookName());
                    cart.setBookPhoto(bookBean.getBookPhoto());
                    cart.setBookPrice(bookBean.getBookPrice());
                    cart.setBookCount(bookCount);
                    cart.setBookAllPrice(bookAllPrice);

                    cart.setBookBean(bookBean);
                    cartList.add(cart);
                } else {
                    request.getRequestDispatcher("cart.jsp").forward(request, response);
                }

            }
            cartBean.setCartAllPrice(cartAllPrice);
            request.setAttribute("cartList", cartList);
            request.getRequestDispatcher("cart.jsp").forward(request, response);
        }

		/*PrintWriter out = response.getWriter();
        out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
		out.println("<HTML>");
		out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
		out.println("  <BODY>");
		out.print("    This is ");
		out.print(this.getClass());
		out.println(", using the GET method");
		out.println("  </BODY>");
		out.println("</HTML>");
		out.flush();
		out.close();*/
    }

    /**
     * The doPost method of the servlet. <br>
     * <p>
     * This method is called when a form has its tag value method equals to post.
     *
     * @param request  the request send by the client to the server
     * @param response the response send by the server to the client
     * @throws ServletException if an error occurred
     * @throws IOException      if an error occurred
     */
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        this.doGet(request, response);
    }

}
